package com.guli.eduucenter.controller;


import com.guli.commonutils.JwtUtils;
import com.guli.commonutils.R;
import com.guli.eduucenter.bean.UcenterMember;
import com.guli.eduucenter.bean.vo.LoginVo;
import com.guli.eduucenter.bean.vo.RegisterVo;
import com.guli.eduucenter.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-12
 */
@RestController
@RequestMapping("/eduucenter/ucenter")
@CrossOrigin
public class UcenterMemberController {
    @Resource
    private UcenterMemberService ucenterMemberService;

    @ApiOperation("登录")
    @PostMapping("login")
    public R login(@RequestBody LoginVo loginVo){
        //登录判断，返回token
        String token = ucenterMemberService.login(loginVo);
        return R.ok().data("token",token);
    }

    @ApiOperation("注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo RegisterVo){
        //注册
        ucenterMemberService.register(RegisterVo);
        return R.ok().message("注册成功");
    }

    @ApiOperation("根据token来获取用户信息")
    @GetMapping("getMemberByToken")
    public R getMemberByToken(HttpServletRequest request){
        //调用工具类
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = ucenterMemberService.getById(memberIdByJwtToken);
        return R.ok().data("userInfo",member);
    }

    @ApiOperation("根据token来获取用户信息")
    @GetMapping("getMemberByTokenToComment")
    public UcenterMember getMemberByTokenToComment(@RequestHeader("token")String token){
        //调用工具类
        System.out.println(token+"))))))))))))))))))))))))))))))))");
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken2(token);
        System.out.println("memberIdByJwtToken:"+memberIdByJwtToken);
        UcenterMember member = ucenterMemberService.getById(memberIdByJwtToken);
        System.out.println("member:"+member);
        return member;
    }

    @ApiOperation("查询一天中的注册人数")
    @GetMapping("getRegisterCountByDate/{day}")
    public Integer getRegisterCountByDate(@PathVariable("day")String day){
        Integer count = ucenterMemberService.getRegisterCountByDate(day);
        System.out.println("**************"+count);
        return count;
    }
}

